<?php

namespace Admin\Controller;
use User\Api\UserApi;

class OrderController extends AdminController {
    public function index($status=''){
        if(!empty($status)){
            $map['status'] = $status;
        }else{
            $status = null;
        }
        $keyword = I('title','');
        if(!empty($keyword)){
            $map['order_no|car_no'] = array('like',"%{$keyword}%");
        }
        $start = I('time-start');
        if(!empty($start)){
            $map['posttime'] = array('egt',$start);
        }
        $end = I('time-end');
        if(!empty($end)){
            $map['posttime'] = array('elt',24*60*60 + strtotime($end));
        }
        
        $list = $this->lists('Order', $map);
        int_to_string($list,array('status'=>array(1=>'已支付',-1=>'已过期',0=>'未支付',2=>'已出保单')));
        $this->assign('status',$status);
        
        $this->assign('_list', $list);
        $this->meta_title = '订单列表';
        
        // 记录当前列表页的cookie
        Cookie('__forward__',$_SERVER['REQUEST_URI']);
        $this->display();
    }
    
    public function add($obdid='',$model_id=''){
        if(IS_POST){
            if(empty($obdid)){
                $this->error("无效的OBD序号");
            }
            if(empty($model_id)){
                $this->error('无效的OBD型号');
            }
            $data['obdid'] = $obdid;
            $data['model_id'] = $model_id;
            $model = M('Obd');
            $ret = $model->add($data);
            if($ret !== false){
                $this->success("保存成功！",U('Obd/index'));
            }else{
                $this->error('数据库写入失败');
            }
            
        }else{
            $model = M('ObdModel');
            $resModels = $model->select();
            $this->assign('models',$resModels);
            $this->meta_title = '新增OBD';
            $this->display();
        }
    }
    
    public function edit($id=''){
        if(empty($id)){
            $this->error('错误的ID');
        }
        if(IS_POST){
            $data['obdid'] = $id;
            $data['model_id'] = I('model_id');
            $model = M('Obd');
            $model->save($data);
            $this->success('操作成功！',U('Obd/index'));
        }else{
            $model = M('ObdModel');
            $resModels = $model->select();
            $this->assign('models',$resModels);

            $model = M('Obd');
            $res = $model->where("obdid=$id")->find();
            $this->assign('obd',$res);
            $this->meta_title = '修改OBD信息';
            $this->display();
        }
    }
    
    public function setStatus($status=''){
        $id = array_unique((array)I('id',0));
        $id = is_array($id) ? implode(',',$id) : $id;
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        $map['order_id'] =   array('in',$id);
        $data['status'] = $status;
        $model = M('Order');
        $model->where($map)->save($data);
        $this->success('操作成功！');
    }
    
    public function del($status=''){
        $id = array_unique((array)I('id',0));
        $id = is_array($id) ? implode(',',$id) : $id;
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        $map['order_id'] = array('in',$id);
        $model = M('Order');
        $model->where($map)->delete();
        $this->success('操作成功！');
    }
    
    public function recycle(){
        $map['status'] = array('lt',0);
        $keyword = I('title','');
        if(!empty($keyword)){
            $map['order_no|car_no'] = array('like',"%{$keyword}%");
        }
        $start = I('time-start');
        if(!empty($start)){
            $map['posttime'] = array('egt',$start);
        }
        $end = I('time-end');
        if(!empty($end)){
            $map['posttime'] = array('elt',24*60*60 + strtotime($end));
        }
        
        $list = $this->lists('Order', $map);
        int_to_string($list,array('status'=>array(1=>'已支付',-1=>'已过期',0=>'未支付',2=>'已出保单')));
        $this->assign('status',$status);
        
        $this->assign('_list', $list);
        $this->meta_title = '已删除订单列表';
        
        // 记录当前列表页的cookie
        Cookie('__forward__',$_SERVER['REQUEST_URI']);
        $this->display();
    }
    
    public function detail($id=''){
        if(empty($id)){
            $this->error('错误的ID号');
        }
        $map['order_id'] = $id;
        $resOrder = M('Order')->where($map)->find();
        if($resOrder){
            $product_id = $resOrder['product_id'];
            $modelProduct = M('Product');
            $resProduct = $modelProduct->where("product_id=$product_id")->find();
            if($resProduct){
                $tableName = $resProduct['order_table'];
                $model = M($tableName);
                $where['corder_id'] = $resOrder['targetid'];
                $resCic = $model->where($where)->find();
                if($resCic){
                    $status = $resCic['status'];
                    if($status > 1){//已支付的话，取保单号
                        $modelNo = M("{$tableName}PolicyList");
                        $resDh = $modelNo->where($where)->select();
                        foreach($resDh as &$row){
                            $row['policyno'] = $row['c_policy_no'];
                        }
                        $this->assign('dh',$resDh);
                    }else{//未支付的话取投保单号
                        $modelNo = M("{$tableName}Dh");
                        $resDh = $modelNo->where($where)->select();
                        foreach($resDh as &$row){
                            $row['policyno'] = $row['c_proposal_no'];
                        }
                        $this->assign('dh',$resDh);
                    }
                    $modelDetail = M("{$tableName}Detail");
                    $resDetail = $modelDetail->where($where)->select();
                    $this->assign('detail',$resDetail);
                    $this->assign('order',$resCic);
                    $this->display();
                }else{
                    $this->error('找不到对应订单信息！');
                }
            }else{
                $this->error('找不到对应的产品！');
            }
        }else{
            $this->error('找不到该保单号！');
        }
    }
}